/*
This file is part of IRCaBot.
IRCaBot is IRC logger with features.
Source code: https://notabug.org/acetone/ircabot.
Copyright (C) acetone, 2023.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include "token.h"

#include <QString>
#include <QMutex>
#include <QSharedPointer>

class VoiceGate
{
public:
    struct Target
    {
        QString network;
        QString nickname;
    };
    struct Captcha
    {
        QString imageB64;
        QString answer;
        QString token;         // for mapping "bad captcha"
        qint64 timestamp = 0;  // for check to timeout
        uint8_t badReportCounter = 0; // can not resolve report by user counter
    };
    struct CaptchaView
    {
        void init(const QSharedPointer<Captcha> captcha, const Target& target, uint8_t currentBadCounter);
        QString imageB64;
        QString badToken;
        QString token;
    };

    VoiceGate() = delete;
    static QString getInviteToken(const Target& target);
    static QSharedPointer<CaptchaView> getCaptcha(const QString& inviteToken, bool* timedout = nullptr, QString* answer /*for debug reasons*/ = nullptr);
    static void badCaptchaReport(const QString& badToken);
    static QSharedPointer<Target> verify(const QString& captchaToken, const QString& answer);
    static void dropCache();

private:
    static QSharedPointer<Captcha> getCachedCaptcha();

    static QMutex m_captchaCacheMutex;
    static QList<QSharedPointer<Captcha>> m_captchaCache;
};

